
using System.Linq;
using System.Web.Mvc;
using Ezio.Models;
using System.Collections.Generic;

namespace Ezio.Controllers
{
    public class ZonasController : Controller
    {
        DataProvider Provider = new DataProvider();
        
        
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Agregar(Zonas Zona)
        {
            Provider.AddToZonas(Zona);
            return RedirectToAction("Index", new { Success = Provider.TrySaveChanges() });
        }
        public ActionResult Index(bool? Success)
        {
            string Message = Success.HasValue ? Success.Value ? "" : "Ha ocurrido un error al procesar su peticion" : "";
            
            ViewData["Zonas"] = Provider.Zonas.ToList();
            ViewData["Departamentos"] = Provider.Departamentos.ToList();
            ViewData["Message"] = Message;
            return View("Index");
        }

        public ActionResult Editar(int Id)
        {
            ViewData["Zona"] = Provider.Zonas.Single(t => t.IdZona == Id);
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public bool Update(Zonas Zona)
        {
            Zonas myZona= Provider.Zonas.Single(t => t.IdZona == Zona.IdZona);
            myZona.Copy(Zona);
            return Provider.TrySaveChanges();
        }

        public ActionResult Eliminar(int Id)
        {
            Provider.Zonas.DeleteObject(Provider.Zonas.Single(t => t.IdZona == Id));
            return RedirectToAction("Index", new { Success = Provider.TrySaveChanges() }); ;
        }
		
		[AcceptVerbs(HttpVerbs.Post)]
        public List<Zonas> GetByDepartamento(int IdDepartamento)
        {
            return Provider.Zonas.Where(t => t.IdDepartamento == IdDepartamento).ToList();
        }
    }
}
